home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / mpeg / util.h < prev    next >
C/C++ Source or Header  |  1994-08-01  |  22KB  |  363 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /* Status codes for bit stream i/o operations. */
  23.  
  24. #define NO_VID_STREAM -1
  25. #define UNDERFLOW -2
  26. #define OK 1
  27.  
  28. /* Size increment of extension data buffers. */
  29.  
  30. #define EXT_BUF_SIZE 1024
  31.  
  32. /* External declarations for bitstream i/o operations. */
  33. extern unsigned int bitMask[];
  34. extern unsigned int nBitMask[];
  35. extern unsigned int rBitMask[];
  36. extern unsigned int bitTest[];
  37.  
  38. /* External declarations of bitstream global variables. */
  39. extern unsigned int curBits;
  40. extern int bitOffset;
  41. extern int bufLength;
  42. extern unsigned int *bitBuffer;
  43.  
  44. /* Macro for updating bit counter if analysis tool is on. */
  45. #ifdef ANALYSIS
  46. #define UPDATE_COUNT(numbits) bitCount += numbits
  47. #else
  48. #define UPDATE_COUNT(numbits)
  49. #endif
  50.  
  51. #ifdef NO_SANITY_CHECKS
  52. #define get_bits1(result)                                                 \
  53. {                                                                         \
  54.   UPDATE_COUNT(1);                                                        \
  55.   result = ((curBits & 0x80000000) != 0);                              \
  56.   curBits <<= 1;                                                          \
  57.   bitOffset++;                                                            \
  58.                                                                           \
  59.   if (bitOffset & 0x20) {                                                 \
  60.     bitOffset = 0;                                                        \
  61.     bitBuffer++;                                                          \
  62.     curBits = *bitBuffer;                                                 \
  63.     bufLength--;                                                          \
  64.   }                                                                       \
  65. }
  66.  
  67. #define get_bits2(result)                                                 \
  68. {                                                                         \
  69.   UPDATE_COUNT(2);                                                        \
  70.   bitOffset += 2;                                                         \
  71.                                                                           \
  72.   if (bitOffset & 0x20) {                                                 \
  73.     bitOffset -= 32;                                                      \
  74.     bitBuffer++;                                                          \
  75.     bufLength--;                                                          \
  76.     if (bitOffset) {                                                      \
  77.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \
  78.     }                                                                     \
  79.     result = ((curBits & 0xc0000000) >> 30);                           \
  80.     curBits = *bitBuffer << bitOffset;                                    \
  81.   }                                                                       \
  82.                                                                           \
  83.   result = ((curBits & 0xc0000000) >> 30);                             \
  84.   curBits <<= 2;                                                          \
  85. }
  86.  
  87. #define get_bitsX(num, mask, shift,  result)                              \
  88. {                                                                         \
  89.   UPDATE_COUNT(num);                                                      \
  90.   bitOffset += num;                                                       \
  91.                                                                           \
  92.   if (bitOffset & 0x20) {                                                 \
  93.     bitOffset -= 32;                                                      \
  94.     bitBuffer++;                                                          \
  95.     bufLength--;                                                          \
  96.     if (bitOffset) {                                                      \
  97.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  98.     }                                                                     \
  99.     result = ((curBits & mask) >> shift);                              \
  100.     curBits = *bitBuffer << bitOffset;                                    \
  101.   }                                                                       \
  102.   else {                                                                  \
  103.     result = ((curBits & mask) >> shift);                              \
  104.     curBits <<= num;                                                      \
  105.   }                                                                       \
  106. }
  107. #else
  108.  
  109. #define get_bits1(result)                                                 \
  110. {                                                                         \
  111.   /* Check for underflow. */                                              \
  112.                                                                           \
  113.   if (bufLength < 2) {                                                    \
  114.     correct_underflow();                                                  \
  115.   }                                                                       \
  116.   UPDATE_COUNT(1);                                                        \
  117.   result = ((curBits & 0x80000000) != 0);                              \
  118.   curBits <<= 1;                                                          \
  119.   bitOffset++;                                                            \
  120.                                                                           \
  121.   if (bitOffset & 0x20) {                                                 \
  122.     bitOffset = 0;                                                        \
  123.     bitBuffer++;                                                          \
  124.     curBits = *bitBuffer;                                                 \
  125.     bufLength--;                                                          \
  126.   }                                                                       \
  127. }
  128.  
  129. #define get_bits2(result)                                                 \
  130. {                                                                         \
  131.   /* Check for underflow. */                                              \
  132.                                                                           \
  133.   if (bufLength < 2) {                                                    \
  134.     correct_underflow();                                                  \
  135.   }                                                                       \
  136.   UPDATE_COUNT(2);                                                        \
  137.   bitOffset += 2;                                                         \
  138.                                                                           \
  139.   if (bitOffset & 0x20) {                                                 \
  140.     bitOffset -= 32;                                                      \
  141.     bitBuffer++;                                                          \
  142.     bufLength--;                                                          \
  143.     if (bitOffset) {                                                      \
  144.       curBits |= (*bitBuffer >> (2 - bitOffset));                         \
  145.     }                                                                     \
  146.     result = ((curBits & 0xc0000000) >> 30);                           \
  147.     curBits = *bitBuffer << bitOffset;                                    \
  148.   }                                                                       \
  149.                                                                           \
  150.   result = ((curBits & 0xc0000000) >> 30);                             \
  151.   curBits <<= 2;                                                          \
  152. }
  153.  
  154. #define get_bitsX(num, mask, shift,  result)                              \
  155. {                                                                         \
  156.   /* Check for underflow. */                                              \
  157.                                                                           \
  158.   if (bufLength < 2) {                                                    \
  159.     correct_underflow();                                                  \
  160.   }                                                                       \
  161.   UPDATE_COUNT(num);                                                      \
  162.   bitOffset += num;                                                       \
  163.                                                                           \
  164.   if (bitOffset & 0x20) {                                                 \
  165.     bitOffset -= 32;                                                      \
  166.     bitBuffer++;                                                          \
  167.     bufLength--;                                                          \
  168.     if (bitOffset) {                                                      \
  169.       curBits |= (*bitBuffer >> (num - bitOffset));                       \
  170.     }                                                                     \
  171.     result = ((curBits & mask) >> shift);                              \
  172.     curBits = *bitBuffer << bitOffset;                                    \
  173.   }                                                                       \
  174.   else {                                                                  \
  175.    result = ((curBits & mask) >> shift);                               \
  176.    curBits <<= num;                                                       \
  177.   }                                                                       \
  178. }
  179. #endif
  180.  
  181. #define get_bits3(result) get_bitsX(3,   0xe0000000, 29, result)
  182. #define get_bits4(result) get_bitsX(4,   0xf0000000, 28, result)
  183. #define get_bits5(result) get_bitsX(5,   0xf8000000, 27, result)
  184. #define get_bits6(result) get_bitsX(6,   0xfc000000, 26, result)
  185. #define get_bits7(result) get_bitsX(7,   0xfe000000, 25, result)
  186. #define get_bits8(result) get_bitsX(8,   0xff000000, 24, result)
  187. #define get_bits9(result) get_bitsX(9,   0xff800000, 23, result)
  188. #define get_bits10(result) get_bitsX(10, 0xffc00000, 22, result)
  189. #define get_bits11(result) get_bitsX(11, 0xffe00000, 21, result)
  190. #define get_bits12(result) get_bitsX(12, 0xfff00000, 20, result)
  191. #define get_bits14(result) get_bitsX(14, 0xfffc0000, 18, result)
  192. #define get_bits16(result) get_bitsX(16, 0xffff0000, 16, result)
  193. #define get_bits18(result) get_bitsX(18, 0xffffc000, 14, result)
  194. #define get_bits32(result) get_bitsX(32, 0xffffffff,  0, result)
  195.  
  196. #define get_bitsn(num, result) get_bitsX((num), nBitMask[num], (32-(num)), result)
  197.  
  198. #ifdef NO_SANITY_CHECKS
  199. #define show_bits32(result)                                      \
  200. {                                                                       \
  201.   if (bitOffset) {                            \
  202.     result = curBits | (*(bitBuffer+1) >> (32 - bitOffset));        \
  203.   }                                                                     \
  204.   else {                                                                \
  205.     result = curBits;                            \
  206.   }                                                                     \
  207. }
  208.  
  209. #define show_bitsX(num, mask, shift,  result)                           \
  210. {                                                                       \
  211.   int bO;                                                               \
  212.   bO = bitOffset + num;                                                 \
  213.   if (bO > 32) {                                                        \
  214.     bO -= 32;                                                           \
  215.     result = ((curBits & mask) >> shift) |                              \
  216.                 (*(bitBuffer+1) >> (shift + (num - bO)));               \
  217.   }                                                                     \
  218.   else {                                                                \
  219.     result = ((curBits & mask) >> shift);                               \
  220.   }                                                                     \
  221. }
  222.  
  223. #else
  224. #define show_bits32(result)                                       \
  225. {                                                                       \
  226.   /* Check for underflow. */                                            \
  227.   if (bufLength < 2) {                                                  \
  228.     correct_underflow();                                                \
  229.   }                                                                     \
  230.   if (bitOffset) {                            \
  231.     result = curBits | (*(bitBuffer+1) >> (32 - bitOffset));        \
  232.   }                                                                     \
  233.   else {                                                                \
  234.     result = curBits;                            \
  235.   }                                                                     \
  236. }
  237.  
  238. #define show_bitsX(num, mask, shift, result)                            \
  239. {                                                                       \
  240.   int bO;                                                               \
  241.                                                                         \
  242.   /* Check for underflow. */                                            \
  243.   if (bufLength < 2) {                                                  \
  244.     correct_underflow();                                                \
  245.   }                                                                     \
  246.   bO = bitOffset + num;                                                 \
  247.   if (bO > 32) {                                                        \
  248.     bO -= 32;                                                           \
  249.     result = ((curBits & mask) >> shift) |                              \
  250.                 (*(bitBuffer+1) >> (shift + (num - bO)));               \
  251.   }                                                                     \
  252.   else {                                                                \
  253.     result = ((curBits & mask) >> shift);                               \
  254.   }                                                                     \
  255. }
  256. #endif
  257.  
  258. #define show_bits1(result)  show_bitsX(1,  0x80000000, 31, result)
  259. #define show_bits2(result)  show_bitsX(2,  0xc0000000, 30, result)
  260. #define show_bits3(result)  show_bitsX(3,  0xe0000000, 29, result)
  261. #define show_bits4(result)  show_bitsX(4,  0xf0000000, 28, result)
  262. #define show_bits5(result)  show_bitsX(5,  0xf8000000, 27, result)
  263. #define show_bits6(result)  show_bitsX(6,  0xfc000000, 26, result)
  264. #define show_bits7(result)  show_bitsX(7,  0xfe000000, 25, result)
  265. #define show_bits8(result)  show_bitsX(8,  0xff000000, 24, result)
  266. #define show_bits9(result)  show_bitsX(9,  0xff800000, 23, result)
  267. #define show_bits10(result) show_bitsX(10, 0xffc00000, 22, result)
  268. #define show_bits11(result) show_bitsX(11, 0xffe00000, 21, result)
  269. #define show_bits12(result) show_bitsX(12, 0xfff00000, 20, result)
  270. #define show_bits13(result) show_bitsX(13, 0xfff80000, 19, result)
  271. #define show_bits14(result) show_bitsX(14, 0xfffc0000, 18, result)
  272. #define show_bits15(result) show_bitsX(15, 0xfffe0000, 17, result)
  273. #define show_bits16(result) show_bitsX(16, 0xffff0000, 16, result)
  274. #define show_bits17(result) show_bitsX(17, 0xffff8000, 15, result)
  275. #define show_bits18(result) show_bitsX(18, 0xffffc000, 14, result)
  276. #define show_bits19(result) show_bitsX(19, 0xffffe000, 13, result)
  277. #define show_bits20(result) show_bitsX(20, 0xfffff000, 12, result)
  278. #define show_bits21(result) show_bitsX(21, 0xfffff800, 11, result)
  279. #define show_bits22(result) show_bitsX(22, 0xfffffc00, 10, result)
  280. #define show_bits23(result) show_bitsX(23, 0xfffffe00,  9, result)
  281. #define show_bits24(result) show_bitsX(24, 0xffffff00,  8, result)
  282. #define show_bits25(result) show_bitsX(25, 0xffffff80,  7, result)
  283. #define show_bits26(result) show_bitsX(26, 0xffffffc0,  6, result)
  284. #define show_bits27(result) show_bitsX(27, 0xffffffe0,  5, result)
  285. #define show_bits28(result) show_bitsX(28, 0xfffffff0,  4, result)
  286. #define show_bits29(result) show_bitsX(29, 0xfffffff8,  3, result)
  287. #define show_bits30(result) show_bitsX(30, 0xfffffffc,  2, result)
  288. #define show_bits31(result) show_bitsX(31, 0xfffffffe,  1, result)
  289.  
  290. #define show_bitsn(num,result) show_bitsX((num), (0xffffffff << (32-(num))), (32-(num)), result)
  291.  
  292. #ifdef NO_SANITY_CHECKS
  293. #define flush_bits32                                                  \
  294. {                                                                     \
  295.   UPDATE_COUNT(32);                                                   \
  296.                                                                       \
  297.   bitBuffer++;                                                        \
  298.   bufLength--;                                                        \
  299.   curBits = *bitBuffer  << bitOffset;                                 \
  300. }
  301.  
  302. #define flush_bits(num)                                               \
  303. {                                                                     \
  304.   bitOffset += num;                                                   \
  305.                                                                       \
  306.   UPDATE_COUNT(num);                                                  \
  307.                                                                       \
  308.   if (bitOffset & 0x20) {                                             \
  309.     bitOffset -= 32;                                                  \
  310.     bitBuffer++;                                                      \
  311.     bufLength--;                                                      \
  312.     curBits = *bitBuffer  << bitOffset;                               \
  313.   }                                                                   \
  314.   else {                                                              \
  315.     curBits <<= num;                                                  \
  316.   }                                                                   \
  317. }
  318. #else
  319. #define flush_bits32                                                  \
  320. {                                                                     \
  321.   if (curVidStream == NULL) {                                         \
  322.     /* Deal with no vid stream here. */                               \
  323.   }                                                                   \
  324.                                                                       \
  325.   if (bufLength < 2) {                                                \
  326.     correct_underflow();                                              \
  327.   }                                                                   \
  328.                                                                       \
  329.   UPDATE_COUNT(32);                                                   \
  330.                                                                       \
  331.   bitBuffer++;                                                        \
  332.   bufLength--;                                                        \
  333.   curBits = *bitBuffer  << bitOffset;                                 \
  334. }
  335.  
  336. #define flush_bits(num)                                               \
  337. {                                                                     \
  338.   if (curVidStream == NULL) {                                         \
  339.     /* Deal with no vid stream here. */                               \
  340.   }                                                                   \
  341.                                                                       \
  342.   if (bufLength < 2) {                                                \
  343.     correct_underflow();                                              \
  344.   }                                                                   \
  345.                                                                       \
  346.   UPDATE_COUNT(num);                                                  \
  347.                                                                       \
  348.   bitOffset += num;                                                   \
  349.                                                                       \
  350.   if (bitOffset & 0x20) {                                             \
  351.     bufLength--;                                                      \
  352.     bitOffset -= 32;                                                  \
  353.     bitBuffer++;                                                      \
  354.     curBits = *bitBuffer << bitOffset;                                \
  355.   }                                                                   \
  356.   else {                                                              \
  357.     curBits <<= num;                                                  \
  358.   }                                                                   \
  359. }
  360. #endif
  361.  
  362. #define UTIL2
  363.